#include "si12_task.h"
#include "buzzer.h"
#include "string.h"
#include "flash_write_read.h"
#include "flash_uid.h"
#include "oled.h"
#include "as608_task.h"

extern uint8_t uid_handle_flag;//加减卡标志 0 常态 1加卡 2减卡
extern uint8_t uid_weigen_flag;//韦根模式标志
extern uint8_t flag;	//按键标志，用于判断哪个按键按下
uint8_t button_buf[8]; //按键数据


uint8_t button_buf_len = 0;//按键数据长度
uint8_t button_press_flag = 0;//判断是否要响按键音




//si12模块初始化
void si12_task_init(void)
{
	MyExti1_init();
	
	SI12TCH_Init();
}

//判断哪个按键按下
void si12_button_press(char *cr)
{
	switch (flag)
	{
		case 1  : { *cr = 1; 	break;  }
		case 2  : { *cr = 2; 	break;  }
		case 3  : { *cr = 3; 	break;  }
		case 4  : { *cr = 4; 	break;  }
		case 5  : { *cr = 5; 	break;  }
		case 6  : { *cr = 6; 	break;  }
		case 7  : { *cr = 7; 	break;  }
		case 8  : { *cr = 8; 	break;  }
		case 9  : { *cr = 9; 	break;  }
		case 12 : 
		{
			*cr = '#';
			if(rfid_schema_flag !=0 && si12_schema_flag == 0)//si12待机，rfid非普通状态
			{
				uid_weigen_flag = 0;//韦根退出
				uid_handle_flag = 0;//普通状态
				rfid_schema_flag = 0;//普通状态 
				oled_rfid_mode(rfid_schema_flag);
        as608_task_flag = 0;//指纹回到验证状态
        as608_schema_flag = 0;//验证状态
        oled_as608_mode(as608_schema_flag);
			}
			break;  
		}
		case 11 : 
		{ 
			
			*cr = 0; 	
			break;  
		}
		case 10 : 
		{ 
			*cr = '*';
			break;  
		}
		default:
		break;
	}
}

//编程模式处理
void schema1(char *cr)
{
//	uint8_t i;
	//判断是否为 *或 #
	if(*cr == '*')//回到待机状态
	{
		si12_schema_flag = 0;
		oled_si12_mode(si12_schema_flag);
		return;
	}
	else if(*cr == '#')//确认
	{
		if(button_buf_len != 6)
		{
			//失败
			si12_schema_flag = 0;
			oled_si12_mode(si12_schema_flag);
			si12_fail();
		}
		else
		{
			if(si12_judge_adminkey())//管理员密钥
			{
				si12_schema_flag = 2;
				oled_si12_mode(si12_schema_flag);
				si12_succced();
			}
			else if(si12_judge_userkey())//用户密钥
			{
				si12_schema_flag = 0;
				oled_si12_mode(si12_schema_flag);
				si12_succced();
			}
			else
			{
				//密钥错误
				si12_schema_flag = 0;
				oled_si12_mode(si12_schema_flag);
				si12_fail();
			}
		}
		//清空数据
		return;
	}
	
	//存入数据缓冲
	if(button_buf_len>=6) button_buf_len = 5;
	button_buf[button_buf_len] = (uint8_t)*cr;
	button_buf_len++;
	oled_updata_key();
}	

//选择模式处理
void schema2(char *cr)
{
	switch(*cr)
	{
		case 0://修改管理员密钥
		{
			si12_schema_flag = 3;
			oled_si12_mode(si12_schema_flag);//刷新状态
			break;
		}
		case 1://修改用户密钥
		{
			si12_schema_flag = 4;
			oled_si12_mode(si12_schema_flag);//刷新状态
			break;
		}
		case 2://加卡
		{
			uid_weigen_flag = 0;//韦根退出
			uid_handle_flag = 1;//加卡状态
			si12_schema_flag = 0;//si12待机
			rfid_schema_flag = 1;//rfid 加卡
			oled_si12_mode(si12_schema_flag);
			oled_rfid_mode(rfid_schema_flag);
			break;
		}
		case 3://减卡
		{
			uid_weigen_flag = 0;//韦根退出
			uid_handle_flag = 2;//减卡状态
			si12_schema_flag = 0;//si12待机
			rfid_schema_flag = 2;//rfid 减卡
			oled_si12_mode(si12_schema_flag);
			oled_rfid_mode(rfid_schema_flag);
			break;
		}
		case 4://韦根
		{
			uid_weigen_flag = 1;//韦根退出
			uid_handle_flag = 0;//退出加减卡状态
			si12_schema_flag = 0;//si12待机
			rfid_schema_flag = 3;//rfid 韦根
			oled_si12_mode(si12_schema_flag);
			oled_rfid_mode(rfid_schema_flag);
			break;
		}
		case 5://加指纹
		{
      as608_task_flag = 1;//指纹回到验证状态
			as608_schema_flag = 1;//加状态
      si12_schema_flag = 0;//si12待机
      oled_si12_mode(si12_schema_flag);//刷新状态
      oled_as608_mode(as608_schema_flag);
      break;
		}
		case 6://减指纹
		{
			as608_task_flag = 2;//指纹回到验证状态
			as608_schema_flag = 2;//减状态
      si12_schema_flag = 0;//si12待机
      oled_si12_mode(si12_schema_flag);//刷新状态
      oled_as608_mode(as608_schema_flag);
      break;
		}
//		case 7:
//		{
//			break;
//		}
//		case 8:
//		{
//			break;
//		}
//		case 9:
//		{
//			break;
//		}
		case '*'://回到待机
		{
//			uid_weigen_flag = 0;
//			uid_handle_flag = 0;
			si12_schema_flag = 0;
			oled_si12_mode(si12_schema_flag);
			break;
		}
//		case '#'://退出
//		{
//			break;
//		}
	}
}




//按键数据处理
void button_data_manage(char *cr)
{
	//等待 *
	if(si12_schema_flag == 0)
	{
		if(*cr == '*')
		{
			si12_schema_flag = 1;
			oled_si12_mode(si12_schema_flag);
		}
		else
		{
			return;
		}
	}
	//否则
	else
	{
		switch(si12_schema_flag)
		{
			case 1 :	schema1(cr);	break;//编程模式
			case 2 :	schema2(cr);	break;//选择功能
			case 3 :	newkey(cr,0);	break;//管理员密钥修改
			case 4 : 	newkey(cr,1);break;//用户密钥修改
		}
	}
}


//任务函数
void si12_task(void)
{	
	char cr;//用于存储这次按键按下的数据
	if(flag != 0)
	{
		si12_button_press(&cr);//判断哪个按键按下
		button_data_manage(&cr);
		
		if(button_press_flag == 0)si12_press();//按下后响应动作，蜂鸣器响动
		else button_press_flag = 0;//清零
		
		flag = 0;//清flag
	}
	
}


//判断管理员密钥 0 不是 1 是
uint8_t si12_judge_adminkey()
{
	KEY admin_key;
	uint8_t i,status = 1;
	//获取密钥
	for(i=0;i<3;i++)
	{
		admin_key.keys[i] = flash_read_2byte(KEY_ADMIN_FLASH_ADDRESS+i*2);
	}
	
	//比对数据
	for(i=0;i<6;i++)
	{
		if(admin_key.key[i] != button_buf[i])
		{
			status = 0;
			break;
		}
	}
	return status;
}


//判断用户密钥	0 不是 1 是
uint8_t si12_judge_userkey(void)
{
	KEY user_key;
	uint8_t i,status = 1;
	//获取密钥
	for(i=0;i<3;i++)
	{
		user_key.keys[i] = flash_read_2byte(KEY_USER_FLASH_ADDRESS+i*2);
	}
	
	//比对数据
	for(i=0;i<6;i++)
	{
		if(user_key.key[i] != button_buf[i])
		{
			status = 0;
			break;
		}
	}
	return status;
}



//密钥修改函数 0 修改管理员密钥 1修改用户密钥
void newkey(char *cr,uint8_t key_class)
{
	if(*cr == '*')//回到待机状态
	{
		si12_schema_flag = 0;
		oled_si12_mode(si12_schema_flag);
		return;
	}
	else if(*cr == '#')//确认
	{
		if(button_buf_len != 6)
		{
			//失败
			si12_schema_flag = 0;
			oled_si12_mode(si12_schema_flag);
			si12_fail();
		}
		else
		{
			if(check_key_exist())//密钥数据不存在，允许改
			{
				change_key(key_class);
				si12_succced();
			}
			else//密钥数据存在，不允许改
			{
				
				si12_fail();
			}
			si12_schema_flag = 0;
			oled_si12_mode(si12_schema_flag);
		}
		si12_schema_flag = 0;
		oled_si12_mode(si12_schema_flag);//回到待机模式
		//清空数据
		return;
	}
	if(button_buf_len>=6) button_buf_len = 5;
	button_buf[button_buf_len] = (uint8_t)*cr;
	button_buf_len++;
	oled_updata_key();
}


//检查数据是否存在 0 存在 1不存在
uint8_t check_key_exist()
{
	//判断是否为管理员密钥
	if(si12_judge_adminkey() == 1)
	{
		return 0;
	}
	//判断是否为用户密钥
	if(si12_judge_userkey() == 1)
	{
		return 0;
	}
	return 1;
}




//修改密钥 0修改管理员密钥 非0修改用户密钥
void change_key(uint8_t mode)
{
	KEY new_key;
	uint8_t i;
	uint16_t size;//第一字节的数据字节数
	uint8_t offset ;//密钥存储位置
	//将数据存入缓冲
	for(i=0;i<6;i++)
	{
		new_key.key[i] = button_buf[i];
	}
	//清空缓冲区
	flash_erase_page(BUFFER_ADDRESS);
	size = (flash_read_byte(UID_KEY_BASE_ADDRESS)*4)+FLASH_OFFSET;
	transfer_page_data(UID_KEY_BASE_ADDRESS,BUFFER_ADDRESS,size);//数据备份到缓冲区
	
	flash_erase_page(UID_KEY_BASE_ADDRESS);//清空存储区第一字节
	//确定偏移
	if(mode)mode = 1;
	offset = 4 + mode*6;
	transfer_page_data(BUFFER_ADDRESS,UID_KEY_BASE_ADDRESS,offset);//写入密钥数据前的数据
	flash_write_2byte(UID_KEY_BASE_ADDRESS+offset,new_key.keys,3);//写入新密钥
	offset = offset +6;//此时数据区的数据字节数
	//写入密钥数据后的数据
	transfer_page_data(BUFFER_ADDRESS+offset,UID_KEY_BASE_ADDRESS+offset,size - offset);
	
}

